package Offer.offer2017;

/**
 * 数值的整数次方
 * Created by 雷文康 on 2017/12/30.
 */
public class Test11 {
    public static void main(String[] args) {
        System.out.println(power(2.0, 3));
    }
    public static double power(double base, int exponent){
        if (equals(base, 0.0) && exponent <= 0){
            return 0.0;
        }
        double result = 1.0;
        int absExponent = exponent;
        if (exponent < 0) {
            absExponent = - exponent;
        }
        for (int i = 1; i <= absExponent; i++) {
            result *= base;
        }
        if (exponent < 0){
            result = 1.0/result;
        }
        return result;

    }

    public static boolean equals(double num1, double num2) {
        if (num1 - num2 < 0.00000000001 && num1 - num2 > -0.000000000001){
            return true;
        }
        return false;
    }
}
